#include "GameObjects.h"

// Override Methods

// INITIALISE
bool CGameObjects::initialise(LJMUUtilShaderTexPlain* pshader, ID3D10Device* pdevice,
								std::wstring ptexfilename, int pspritewidth, int pspriteheight)
{
	this->_obj_shader	= pshader;
	this->_obj_sprite	= new LJMUUtilSimpleSprite();

	this->_obj_sprite->initialise(pdevice, ptexfilename, pspritewidth, pspriteheight);

	return true;
}

// RENDER
bool CGameObjects::render(float ptpf, D3DXMATRIX pview, D3DXMATRIX portho)
{
	// render the sprite objects
	this->_obj_sprite->render();
	this->_obj_shader->render(this->_obj_sprite->getIndexCount(), _sprite_world,
								pview, portho, this->_obj_sprite->getTexture());

	return true;
}

// UPDATE
bool CGameObjects::update(float ptpf)
{
	// transform Sprite objects
	D3DXMatrixTranslation(&_sprite_trans, this->_x_pos, this->_y_pos, DEFAULT_Z_POS);
	D3DXMatrixRotationZ(&_sprite_rot, D3DXToRadian(this->_angle));
	D3DXMatrixScaling(&_sprite_scale, this->_x_scale, this->_y_scale, DEFAULT_Z_SCALE);

	// update the sprite object
	this->_sprite_world	= this->_sprite_scale * this->_sprite_rot * this->_sprite_trans;

/*
	// transform rect objects
	D3DXMatrixTranslation(&_rect_trans, this->_x_pos, this->_y_pos, DEFAULT_Z_POS);
	D3DXMatrixRotationZ(&_rect_rot, D3DXToRadian(this->_angle));
	D3DXMatrixScaling(&_rect_scale, this->_x_scale, this->_y_scale, DEFAULT_Z_SCALE);
	// update the rect objects
	this->_rect_world	= this->_rect_scale	* this->_rect_rot	* this->_rect_trans;
*/	

	// Update the RECT with the Sprite
	this->setRect();

	return true;
}

// CLEANUP
bool CGameObjects::cleanup()
{
	this->_obj_sprite->cleanup();
	this->_obj_sprite	= NULL;
	this->_obj_shader	= NULL;

	return true;
}

// Public Methods
float CGameObjects::getWidth()
{
	return this->_obj_sprite->getWidth();
}
/************************************/
float CGameObjects::getHeight()
{
	return this->_obj_sprite->getHeight();
}
/************************************/
float CGameObjects::getHalfWidth()
{
	return this->_obj_sprite->getHalfWidth();
}
/************************************/
float CGameObjects::getHalfHeight()
{
	return this->_obj_sprite->getHalfHeight();
}
/************************************/
void CGameObjects::setScale(float pscale)
{
	this->setXScale(pscale);
	this->setYScale(pscale);
}
/************************************/
void CGameObjects::setPos(float px, float py)
{
	this->setXPos(px);
	this->setYPos(py);
}
/************************************/
void CGameObjects::addScale(float pscale)
{
	this->setXScale(this->getXScale() + pscale);
	this->setYScale(this->getYScale() + pscale);
}
/************************************/
void CGameObjects::addPos(float px, float py)
{
	this->setXPos(this->getXPos() + px);
	this->setYPos(this->getYPos() + py);
}
/************************************/
void CGameObjects::addRot(float prot)
{
	this->setRot(this->getRot() + prot);
}
/************************************/
void CGameObjects::setRect()
{
	/*
		RECT.Left	=	Sprite top-left corner X coord
		RECT.Top	=	Sprite top-left corner Y coord
		RECT.Right =	Sprite btm-right corner X coord
		RECT.Bottom=	Sprite btm-right corner Y coord
	*/

	int txpos       = this->getXPos();
	int typos       = this->getYPos();
	int thalfheight = this->getHalfHeight();
	int thalfwidth  = this->getHalfWidth();

	_rect.left		= txpos - thalfwidth;
	_rect.top		= typos - thalfheight;
	_rect.right		= txpos + thalfwidth;
	_rect.bottom	= typos + thalfheight;

}
/************************************/
RECT& CGameObjects::getRect()
{
	return this->_rect;
}
/************************************/
void CGameObjects::resetPos(float px, float py, float pangle)
{
	this->_x_pos = rand() % 800;
	this->_y_pos = rand() % 600;
	this->_angle += 90;
}
/************************************/

// Internal Method
void CGameObjects::setDefaults()
{
	// Set Default values
	this->_x_pos	= 0;
	this->_y_pos	= 0;
	this->_angle	= 0;
	this->_x_scale	= 1;
	this->_y_scale	= 1;
	this->_active	= true;

	// Set Matrices to Identity Matrix
	D3DXMatrixIdentity(&this->_sprite_rot);
	D3DXMatrixIdentity(&this->_sprite_trans);
	D3DXMatrixIdentity(&this->_sprite_scale);
	D3DXMatrixIdentity(&this->_sprite_world);
/*
	// for rect
	D3DXMatrixIdentity(&this->_rect_rot);
	D3DXMatrixIdentity(&this->_rect_trans);
	D3DXMatrixIdentity(&this->_rect_scale);
	D3DXMatrixIdentity(&this->_rect_world);
*/
}